package controller

import (
	"github.com/gin-gonic/gin"
	"k9-panel/app/adapter/http/internal/convert"
	"k9-panel/app/adapter/internal"
	"k9-panel/app/response"
	"k9-panel/app/service"
	"net/http"
)

var (
	DerivedConfig *DerivedConfigController
)

func init() {
	DerivedConfig = new(DerivedConfigController)
}

type DerivedConfigController struct {
}

func (t *DerivedConfigController) Get(c *gin.Context) {
	var (
		baseRead internal.ConfigReadParam
	)
	if err := c.ShouldBind(&baseRead); err != nil {
		panic(err)
	}
	configs, err := service.DerivedConfig.GetByProject(uint(baseRead.ProjectId), baseRead.EnvironmentId)
	if err != nil {
		panic(err)
	}
	c.JSON(http.StatusAccepted, response.Success(configs))
	return
}

func (t *DerivedConfigController) Update(c *gin.Context) {
	var (
		baseAdd internal.ConfigDataParam
	)
	if err := c.ShouldBind(&baseAdd); err != nil {
		panic(err)
	}
	err := baseAdd.Valid()
	if err != nil {
		panic(err)
	}
	param, _ := convert.ConfigDataParam2DerivedAddParam(&baseAdd)

	create, err := service.DerivedConfig.UpdateByParam(param)
	if err != nil {
		panic(err)
	}
	c.JSON(http.StatusAccepted, response.Success(create))
}
func (t *DerivedConfigController) Preview(c *gin.Context) {
	var (
		baseAdd internal.ConfigDataParam
	)
	if err := c.ShouldBind(&baseAdd); err != nil {
		panic(err)
	}
	create, err := service.DerivedConfig.ViewProject(uint(baseAdd.ProjectId), baseAdd.EnvironmentId)
	if err != nil {
		panic(err)
	}
	c.JSON(http.StatusAccepted, response.Success(create))
}
